<?php
/**
 * Clase Base FN para BaseSoOrdenLineaFN
 *
 * @package    
 * @subpackage BaseSoOrdenLineaFN
 * @author     hans
 * @version    SVN:: BaseSoOrdenLineaFN.class.php,v 1.1 2012-05-07 00:52:41 Hans.parra Exp $
 */

require_once('class/so/ordenes/BN/SoOrdenLineaBN.class.php');

class BaseSoOrdenLineaFN extends SoOrdenLineaBN
{
  private static $fieldNames = array (
    'SoId'=>'sol_soId', 'LineaId'=>'sol_lineaId', 'ArtId'=>'sol_artId', 'ArtNombre'=>'sol_artNombre', 'AlmId'=>'sol_almId', 'Cant'=>'sol_cant', 'CantBckOrd'=>'sol_cantBckOrd', 'UoM'=>'sol_UoM', 'Precio'=>'sol_precio', 'PrecioMn'=>'sol_precio_mn', 'Impuesto'=>'sol_impuesto', 'Subtotal'=>'sol_subtotal', 'Obs'=>'sol_obs', 'CreaUsr'=>'sol_creaUsr', 'CreaFecha'=>'sol_creaFecha', 'ModifUsr'=>'sol_modifUsr', 'ModifFecha'=>'sol_modifFecha'  
  );

  public function select()
  {
    try
    {
      /*$tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;*/

      $where = $this->buildConsult();
      //$orden = $this->buildOrderByField();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      //$sql = "SELECT sol_soId, sol_lineaId, sol_artId, sol_artNombre, sol_almId, sol_cant, sol_cantBckOrd, sol_UoM, sol_precio, sol_precio_mn, sol_impuesto, sol_subtotal, sol_obs, sol_creaUsr, sol_creaFecha, sol_modifUsr, sol_modifFecha FROM so_orden_linea WHERE 1 ".$where." ".$orden."  LIMIT ".$limitInf.",".$tamPag;
      $sql = "SELECT sol_soId, sol_lineaId, sol_artId, sol_artNombre, sol_almId, sol_cant, sol_cantBckOrd, sol_UoM, sol_precio, sol_precio_mn, sol_impuesto, sol_subtotal, sol_obs, sol_creaUsr, sol_creaFecha, sol_modifUsr, sol_modifFecha FROM so_orden_linea WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT sol_soId, sol_lineaId, sol_artId, sol_artNombre, sol_almId, sol_cant, sol_cantBckOrd, sol_UoM, sol_precio, sol_precio_mn, sol_impuesto, sol_subtotal, sol_obs, sol_creaUsr, sol_creaFecha, sol_modifUsr, sol_modifFecha FROM so_orden_linea WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildConsult()
  {
    try
    {
      $where = "";

      if($this->getSoId()){ $where .= " AND sol_soid = '".$this->getSoId()."'"; }
      if($this->getLineaId()){ $where .= " AND sol_lineaid = '".$this->getLineaId()."'"; }
      if($this->getArtId()){ $where .= " AND sol_artid = '".$this->getArtId()."'"; }
      if($this->getArtNombre()){ $where .= " AND sol_artnombre = '".$this->getArtNombre()."'"; }
      if($this->getAlmId()){ $where .= " AND sol_almid = '".$this->getAlmId()."'"; }
      if($this->getCant()){ $where .= " AND sol_cant = '".$this->getCant()."'"; }
      if($this->getCantBckOrd()){ $where .= " AND sol_cantbckord = '".$this->getCantBckOrd()."'"; }
      if($this->getUoM()){ $where .= " AND sol_uom = '".$this->getUoM()."'"; }
      if($this->getPrecio()){ $where .= " AND sol_precio = '".$this->getPrecio()."'"; }
      if($this->getPrecioMn()){ $where .= " AND sol_precio_mn = '".$this->getPrecioMn()."'"; }
      if($this->getImpuesto()){ $where .= " AND sol_impuesto = '".$this->getImpuesto()."'"; }
      if($this->getSubtotal()){ $where .= " AND sol_subtotal = '".$this->getSubtotal()."'"; }
      if($this->getObs()){ $where .= " AND sol_obs = '".$this->getObs()."'"; }
      if($this->getCreaUsr()){ $where .= " AND sol_creausr = '".$this->getCreaUsr()."'"; }
      if($this->getCreaFecha()){ $where .= " AND sol_creafecha = '".$this->getCreaFecha()."'"; }
      if($this->getModifUsr()){ $where .= " AND sol_modifusr = '".$this->getModifUsr()."'"; }
      if($this->getModifFecha()){ $where .= " AND sol_modiffecha = '".$this->getModifFecha()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";

      if(property_exists($this,'sortCampos')){
      if($this->sortCampos != ""){
        $a_valCampos = explode(";", $this->sortCampos);
        $a_valtype   = explode(";", $this->sortType);
        $order_by = " ORDER BY ";
        $a_order = array();
        foreach ($a_valCampos as $key => $val){
          $sTmpOrder = "";
          if(array_key_exists($val, self::$fieldNames)){
            $sTmpOrder .= self::$fieldNames[$val];
            switch ($a_valtype[$key]){
              case 1: $sTmpOrder .= " ASC"; break;
              case 2: $sTmpOrder .= " DESC"; break;
            }
            $a_order[] = $sTmpOrder;
          }
        }
        $order_by .= implode(", ", $a_order);
      }
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* B�squeda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT sol_soId, sol_lineaId, sol_artId, sol_artNombre, sol_almId, sol_cant, sol_cantBckOrd, sol_UoM, sol_precio, sol_precio_mn, sol_impuesto, sol_subtotal, sol_obs, sol_creaUsr, sol_creaFecha, sol_modifUsr, sol_modifFecha FROM so_orden_linea WHERE 1 AND sol_soId='".$this->getSoId()."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      

      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_BaseSoOrdenLineaFN = new SoOrdenLineaBN();
    $o_BaseSoOrdenLineaFN->setSoId($o_propel->sol_soId);
    $o_BaseSoOrdenLineaFN->setLineaId($o_propel->sol_lineaId);
    $o_BaseSoOrdenLineaFN->setArtId($o_propel->sol_artId);
    $o_BaseSoOrdenLineaFN->setArtNombre($o_propel->sol_artNombre);
    $o_BaseSoOrdenLineaFN->setAlmId($o_propel->sol_almId);
    $o_BaseSoOrdenLineaFN->setCant($o_propel->sol_cant);
    $o_BaseSoOrdenLineaFN->setCantBckOrd($o_propel->sol_cantBckOrd);
    $o_BaseSoOrdenLineaFN->setUoM($o_propel->sol_UoM);
    $o_BaseSoOrdenLineaFN->setPrecio($o_propel->sol_precio);
    $o_BaseSoOrdenLineaFN->setPrecioMn($o_propel->sol_precio_mn);
    $o_BaseSoOrdenLineaFN->setImpuesto($o_propel->sol_impuesto);
    $o_BaseSoOrdenLineaFN->setSubtotal($o_propel->sol_subtotal);
    $o_BaseSoOrdenLineaFN->setObs($o_propel->sol_obs);
    $o_BaseSoOrdenLineaFN->setCreaUsr($o_propel->sol_creaUsr);
    $o_BaseSoOrdenLineaFN->setCreaFecha($o_propel->sol_creaFecha);
    $o_BaseSoOrdenLineaFN->setModifUsr($o_propel->sol_modifUsr);
    $o_BaseSoOrdenLineaFN->setModifFecha($o_propel->sol_modifFecha);
    return $o_BaseSoOrdenLineaFN;
  }
}